package com.luming.ethtran.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luming.ethtran.domain.DTO.CostCountDto;
import com.luming.ethtran.domain.EthData;
import com.luming.ethtran.domain.DTO.ReceiverCountDto;
import com.luming.ethtran.domain.Result;
import com.luming.ethtran.domain.DTO.SenderCountDto;
import com.luming.ethtran.mapper.EthDataMapper;
import com.luming.ethtran.service.IEthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Luming
 * @Date 2024/9/2 15:14
 * @Description:
 */
@Service
public class EthDataServiceImpl implements IEthDataService {
    
    @Autowired
    private EthDataMapper ethDataMapper;
    
    @Override
    public Result add(EthData ethData) {
        
        int i = ethDataMapper.insert(ethData);
        
        Result result = new Result<>();
        if (i == 1) {
            result.setCode(200);
            result.setMsg("成功插入数据");
            return result;
        }
        result.setCode(503);
        result.setMsg("插入数据失败");
        return result;
    }
    
    @Override
    public Result delete(Integer id) {
        int i = ethDataMapper.deleteById(id);
        Result result = new Result<>();
        if (i > 0) {
            result.setCode(200);
            result.setMsg("删除成功");
            return result;
        }
        result.setCode(504);
        result.setMsg("删除失败");
        return result;
    }
    
    @Override
    public Result update(EthData ethData) {
        int i = ethDataMapper.updateById(ethData);
        Result result = new Result<>();
        if (i > 0) {
            result.setCode(200);
            result.setMsg("更新成功");
            return result;
        }
        result.setCode(504);
        result.setMsg("更新失败");
        return result;
    }
    
    @Override
    public Result select(Integer id) {
        EthData ethData = ethDataMapper.selectById(id);
        Result result = new Result<>();
        if (ethData == null) {
            result.setCode(500);
            result.setMsg("不存在该交易");
            return result;
        }
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(ethData);
        return result;
    }
    
    @Override
    public Result selectList(EthData ethData, int pageNum, int pageSize) {
        // 创建分页对象
        Page<EthData> page = new Page<>(pageNum, pageSize);
        
        // 使用LambdaQueryWrapper动态构建查询条件
        LambdaQueryWrapper<EthData> queryWrapper = new LambdaQueryWrapper<>();
        
        // 动态拼接查询条件
        if (ethData.getSender() != null && !ethData.getSender().isEmpty()) {
            queryWrapper.eq(EthData::getSender, ethData.getSender());
        }
        if (ethData.getReceiver() != null && !ethData.getReceiver().isEmpty()) {
            queryWrapper.eq(EthData::getReceiver, ethData.getReceiver());
        }
        if (ethData.getCost() != null && !ethData.getCost().isEmpty()) {
            queryWrapper.eq(EthData::getCost, ethData.getCost());
        }
        // 执行分页查询
        Page<EthData> resultPage = ethDataMapper.selectPage(page, queryWrapper);
        
        //判断结果
        Result result = new Result<>();
        if (resultPage.getRecords().isEmpty()) {
            result.setCode(505);
            result.setMsg("未找到相关数据");
            return result;
        }
        result.setCode(200);
        result.setMsg("查找成功");
        result.setData(resultPage);
        return result;
    }
    
    @Override
    public Result selectSender(String sender) {
        LambdaQueryWrapper<EthData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EthData::getSender, sender);
        List<EthData> senderList = ethDataMapper.selectList(wrapper);
        //判断结果
        Result result = new Result<>();
        if (senderList.isEmpty()) {
            result.setCode(505);
            result.setMsg("未找到相关数据");
            return result;
        }
        result.setCode(200);
        result.setMsg("查找成功");
        result.setData(senderList);
        return result;
    }
    
    @Override
    public Result selectReceiver(String receiver) {
        LambdaQueryWrapper<EthData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EthData::getReceiver, receiver);
        List<EthData> senderList = ethDataMapper.selectList(wrapper);
        //判断结果
        Result result = new Result<>();
        if (senderList.isEmpty()) {
            result.setCode(505);
            result.setMsg("未找到相关数据");
            return result;
        }
        result.setCode(200);
        result.setMsg("查找成功");
        result.setData(senderList);
        return result;
    }
    
    @Override
    public Result countBySenders(String s1, String s2, String s3, String s4) {
        List<SenderCountDto> dataList = ethDataMapper.countBySenders(s1, s2, s3, s4);
        //判断结果
        Result result = new Result<>();
        if (dataList.isEmpty()) {
            result.setCode(505);
            result.setMsg("未找到相关数据");
            return result;
        }
        result.setCode(200);
        result.setMsg("查找成功");
        result.setData(dataList);
        return result;
    }
    
    @Override
    public Result countByReceiver(String r1, String r2, String r3, String r4) {
        List<ReceiverCountDto> dataList = ethDataMapper.countByReceiver(r1, r2, r3, r4);
        //判断结果
        Result result = new Result<>();
        if (dataList.isEmpty()) {
            result.setCode(505);
            result.setMsg("未找到相关数据");
            return result;
        }
        result.setCode(200);
        result.setMsg("查找成功");
        result.setData(dataList);
        return result;
    }
    
    @Override
    public Result getCostDistribution() {
        List<CostCountDto> costList = ethDataMapper.getCostDistribution();
        //判断结果
        Result result = new Result<>();
        if (costList.isEmpty()) {
            result.setCode(505);
            result.setMsg("未找到相关数据");
            return result;
        }
        result.setCode(200);
        result.setMsg("查找成功");
        result.setData(costList);
        return result;
    }
    
}
